from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import declarative_base, sessionmaker

from database.mysql_utils import MysqlUtils

Base = declarative_base()


class Store(Base):
    __tablename__ = 'store'
    id = Column(Integer, primary_key=True)
    sid = Column(Integer)
    mid = Column(Integer)
    name = Column(String(255))
    seller_id = Column(String(255))
    account_name = Column(String(255))
    seller_account_id = Column(Integer)
    region = Column(String(255))
    country = Column(String(255))
    has_ads_setting = Column(Integer)
    marketplace_id = Column(String(255))
    status = Column(Integer)


Base.metadata.create_all(MysqlUtils.mysql_connect())
Session = sessionmaker(bind=MysqlUtils.mysql_connect())


class StoreData:

    @staticmethod
    def insert_store_info(all_store_data):
        session = Session()
        for one_store_info in all_store_data:
            sid = one_store_info.get('sid', "")
            mid = one_store_info.get('mid')
            name = one_store_info.get('name', "")
            seller_id = one_store_info.get('seller_id', "")
            account_name = one_store_info.get('account_name', "")
            seller_account_id = one_store_info.get('seller_account_id')
            region = one_store_info.get('region', "")
            country = one_store_info.get('country', "")
            has_ads_setting = one_store_info.get('sid', "")
            marketplace_id = one_store_info.get('marketplace_id', "")
            status = one_store_info.get('status', 0)
            store = Store(
                sid=sid,
                mid=mid,
                name=name,
                seller_id=seller_id,
                account_name=account_name,
                seller_account_id=seller_account_id,
                region=region,
                country=country,
                has_ads_setting=has_ads_setting,
                marketplace_id=marketplace_id,
                status=status
            )
            session.add(store)
        session.commit()
        session.close()

    @staticmethod
    def del_store_info():
        session = Session()
        session.query(Store).delete()
        session.commit()
        session.close()

    @staticmethod
    def get_sid_list():
        session = Session()
        sid_obj = session.query(Store.sid).all()
        session.close()
        sid_list = [sid[0] for sid in sid_obj]
        return sid_list

    @staticmethod
    def get_store_name_by_sid(sid):
        session = Session()
        ret = session.query(Store.name).filter(Store.sid == sid).first()
        session.close()
        print(ret[0])
        return ret[0]


if __name__ == '__main__':
    # StoreData.get_store_name_by_sid("10896")
    StoreData.del_store_info()
